package com.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.baselibrary.Constants;
import com.baselibrary.R;
import com.liaoinstan.springview.utils.DensityUtil;


/**
 * @author 关忆北丶
 * @description: 整个Item高是否超出一屏幕，超出一屏幕，将视频控件的高缩小,缩小多到整个Item高平行充满一屏幕
 * 不超出一屏 则匹配宽度
 * @date :2020/9/16 16:40
 */
public class ScreenUtils {

    private static DisplayMetrics sDM = Resources.getSystem().getDisplayMetrics();

    public static int getScreenWidth() {
        return sDM.widthPixels;
    }

    public static int getScreenHeight() {
        return sDM.heightPixels;
    }

    public static float getDensity() {
        return sDM.density;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * split[0]  宽
     * split[1]  高
     *
     * @param coverUrl
     * @return
     */
    public static String getVideoHight(String coverUrl) {
        try {
//        1280  是宽
//        720 是高
//        http://store.hankeapp.cn/storage/1280*720*131f3cf1fb42f1ca2ba6160327acbead.jpg
            int lastIndexOfHead = coverUrl.lastIndexOf("/");
            int lastIndexOfFoot = coverUrl.lastIndexOf("*");
            String resolving = coverUrl.substring(lastIndexOfHead + 1, lastIndexOfFoot);
            return resolving.split("[*]")[1];
        } catch (Exception e) {
            e.getMessage();
        }
        return "0";
    }

    //竖版海报分享高度
    public static int getVerticalPosterHeight() {
        return (getScreenWidth() - dpToPx(72)) * 384 / 303;
    }

    //横版海报分享高度
    public static int getHorizonPosterHeight() {
        return (getScreenWidth() - dpToPx(72)) * 226 / 303;
    }

    //矩形海报分享高度
    public static int getSquarePosterHeight() {
        return (getScreenWidth() - dpToPx(72));
    }

    /**
     * split[0]  宽
     * split[1]  高
     *
     * @param coverUrl
     * @return
     */
    public static String getVideoWidth(String coverUrl) {
//        1280  是宽
//        720 是高
//        http://store.hankeapp.cn/storage/1280*720*131f3cf1fb42f1ca2ba6160327acbead.jpg
        try {
            int lastIndexOfHead = coverUrl.lastIndexOf("/");
            int lastIndexOfFoot = coverUrl.lastIndexOf("*");
            String resolving = coverUrl.substring(lastIndexOfHead + 1, lastIndexOfFoot);
            return resolving.split("[*]")[0];
        } catch (Exception e) {
            e.getMessage();
        }
        return "0";

    }

    public static boolean isHorizonPicture(String coverUrl) {
        http:
//store.hankeapp.cn/storage/1280*720*131f3cf1fb42f1ca2ba6160327acbead.jpg
        try {
            int lastIndexOfHead = coverUrl.lastIndexOf("/");
            int lastIndexOfFoot = coverUrl.lastIndexOf("*");
            String[] result = coverUrl.substring(lastIndexOfHead + 1, lastIndexOfFoot).split("[*]");
            if (Integer.parseInt(result[0]) > Integer.parseInt(result[1])) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return false;
    }


    /**
     * 话题详情顶部 设置图片高款
     *
     * @param imageView
     * @param width
     * @param height
     * @param maxHeight
     */
    public static void setBitOriginal(Context context, ImageView imageView, int width, int height, int maxHeight) {
        if (height > maxHeight) {
            height = maxHeight;
        }
        width = getScreenWidth(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ViewGroup.LayoutParams mParams = imageView.getLayoutParams();
        mParams.height = height;
        mParams.width = width;
        imageView.setLayoutParams(mParams);
    }


    /**
     * @param itemLayout
     * @param headLayout
     * @param topicLayout
     * @param bottomLayout
     * @param descLayout    首先计算出原有图片的宽高比例
     *                      将高度缩小后，用缩小的数值 除 缩放比例
     * @param viewBackBlack
     */
    public static void getItemHeight(LinearLayout itemLayout, RelativeLayout headLayout, LinearLayout topicLayout, LinearLayout bottomLayout, TextView descLayout,
                                     ImageView videoCover, View viewBackBlack, int videoHeight, int videoWidth, Context activity) {
        float mCutRatio = 0;//宽高比例
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        int mHeadLayoutHeight = headLayout.getHeight();
        int mTopicLayoutHeight = topicLayout.getHeight();
        int mBottomLayoutHeight = bottomLayout.getHeight();
        int mDescLayoutHeight = descLayout.getHeight();
        int mBottomTabHeight = DensityUtil.dip2px(activity, 60) * 2;//这里的 *2  包含的底部的tab  和 顶部的状态栏
        int mTopTabHeight = DensityUtil.dip2px(activity, 45) * 2;//这里的45*2 代表的是顶部的tab  广场/约居/好友  tab 和 广场的 关注/推荐/此刻/热门话题 tab

        int maxVideoHeight = (int) (mScreenHeight - mBottomTabHeight - mTopTabHeight - (mScreenHeight - mBottomTabHeight - mTopTabHeight) * 0.2);//视频展示的最大高度

        int newVideoWidth = 0;
        int newVideoHeight = 0;

        if (videoWidth >= mScreenWidth) {//如果视频宽度超过一屏幕
            mCutRatio = videoWidth / mScreenWidth;
            newVideoHeight = (int) (videoHeight / mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {
                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = (int) (videoWidth / mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (int) (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }

            }
            setLayoutParams(videoCover, viewBackBlack, newVideoWidth, newVideoHeight);
        } else if (videoWidth < mScreenWidth) {//如果视频宽度不超过一屏幕，铺满屏幕，等比例放大高，，如果高超过一屏幕，按照最大高设置
            mCutRatio = mScreenWidth / videoWidth;
            newVideoHeight = (int) (videoHeight * mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {

                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = (int) (videoWidth / mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (int) (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }


            }
            setLayoutParams(videoCover, viewBackBlack, newVideoWidth, newVideoHeight);
        }

    }

    /**
     * 关注列表 图片动态--- 将高度缩小后，用缩小的数值 除 缩放比例
     * <p>
     * 这个是外层ViewPager得高，这里只计算最大高就行
     *
     * @return
     */
    public static int setLocusViewPagerHeight(
            View videoCover, float videoHeight, float videoWidth, float followListHeadHeight, Context activity) {
        float mCutRatio = 0;//宽高比例
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        int mBottomTabHeight = DensityUtil.dip2px(activity, 60) * 2;//这里的 *2  包含的底部的tab  和 顶部的状态栏
        int maxVideoHeight = (int) (mScreenHeight - mBottomTabHeight - followListHeadHeight - ((mScreenHeight - mBottomTabHeight - followListHeadHeight) * 0.2));//展示的最大高度

        float newVideoWidth = 0;
        float newVideoHeight = 0;
        //这里得宽是ViewPager得宽,,ViewPager永远充满一屏幕
        if (videoWidth >= mScreenWidth) {//如果视频宽度超过一屏幕
            mCutRatio = videoWidth / mScreenWidth;//计算缩放比例
            newVideoHeight = (videoHeight / mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {//匹配完宽读，，计算的高度超过最大高度。。则换计算方法，优先匹配高宽
                if (videoHeight >= maxVideoHeight) {//这里的宽不需要压缩
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = videoHeight;
                }

            }

        } else {//如果视频宽度不超过一屏幕，铺满屏幕，等比例放大高，，如果高超过一屏幕，按照最大高设置
            mCutRatio = mScreenWidth / videoWidth;
            newVideoHeight = (videoHeight * mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {//如果宽大于最大宽了
                if (videoHeight > maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = videoHeight;
                }
            }
        }
        ViewGroup.LayoutParams linearParams = videoCover.getLayoutParams();
        linearParams.width = (int) Math.ceil(newVideoWidth);
        linearParams.height = (int) Math.ceil(newVideoHeight);

        videoCover.setLayoutParams(linearParams); //使设置好的布局参数应用到控件


        return (int) Math.ceil(newVideoHeight);
    }


    /**
     * 长图
     *
     * @return
     */
    public static int setLongGraphPagerHeight(
            View videoCover, float videoHeight, float videoWidth, float followListHeadHeight, Context activity) {
        float mCutRatio = 0;//宽高比例
        float newVideoWidth = 0;//宽高比例
        float newVideoHeight = 0;//宽高比例
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        if (mScreenWidth > videoWidth) {
            mCutRatio = mScreenWidth / videoWidth;
            newVideoHeight = videoHeight * mCutRatio;
            newVideoWidth = mScreenWidth;
        } else if (mScreenWidth < videoWidth) {
            mCutRatio = videoWidth / mScreenWidth;
            newVideoHeight = videoHeight / mCutRatio;
            newVideoWidth = mScreenWidth;
        } else {
            newVideoWidth = mScreenWidth;
            newVideoHeight = videoHeight;
        }

        ViewGroup.LayoutParams linearParams = videoCover.getLayoutParams();
        linearParams.width = (int) Math.ceil(newVideoWidth);
        linearParams.height = (int) Math.ceil(newVideoHeight);

        videoCover.setLayoutParams(linearParams); //使设置好的布局参数应用到控件


        return (int) Math.ceil(newVideoHeight);
    }


    /**
     * ViewPager 内部 图片动态
     */
    public static void setLocusItemHeight(View videoCover, ImageView showImg, float videoHeight, float videoWidth, float maxVideoHeight, Context activity) {
        float mCutRatio = 0;//宽高缩放比
        float mScreenWidth = getScreenWidth(activity) - dip2px(activity, 28);//屏幕宽 - 两边的marge
        float newVideoWidth = 0;
        float newVideoHeight = 0;

        if (videoWidth / videoHeight < Constants.DYNAMIC_PICTURE_SCALE) {
            if (videoWidth > mScreenWidth) {
                mCutRatio = videoWidth / mScreenWidth;//计算出裁剪比例
                newVideoHeight = videoHeight / mCutRatio;
                newVideoWidth = mScreenWidth;
                //如果宽/高，不是0.75，说明不是3:4的比例。因为宽固定了，所以只能改变高，达到3:4
                if (newVideoWidth / newVideoHeight != Constants.DYNAMIC_PICTURE_SCALE) {
                    newVideoHeight = newVideoWidth / Constants.DYNAMIC_PICTURE_SCALE;
                }

            } else if (videoWidth <= mScreenWidth) {
                newVideoWidth = mScreenWidth;//这里的赋值其实没有意义，只为最后统一使用  newVideoHeight、newVideoWidth
                if (videoHeight > mScreenWidth / Constants.DYNAMIC_PICTURE_SCALE) {
                    //如果View高，大于屏幕宽/measureScale
                    newVideoHeight = mScreenWidth / Constants.DYNAMIC_PICTURE_SCALE;//
                } else {
                    newVideoHeight = videoHeight;
                }


            }
        } else {
            newVideoHeight = videoHeight;
            newVideoWidth = videoWidth;
            //以下不足一屏扩充，大于一屏缩小
            if (newVideoWidth < mScreenWidth) {
                //扩充一屏，高按比例扩大
                mCutRatio = mScreenWidth / newVideoWidth;
                newVideoHeight = newVideoHeight * mCutRatio;
                newVideoWidth = mScreenWidth;
            } else if (newVideoWidth > mScreenWidth) {
                //缩放到一屏大小，高按比例缩小
                mCutRatio = newVideoWidth / mScreenWidth;
                newVideoHeight = newVideoHeight / mCutRatio;
                newVideoWidth = mScreenWidth;
            }
        }

        //showImg.setScaleType(ImageView.ScaleType.FIT_CENTER);
        RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) videoCover.getLayoutParams();
        linearParams.width = (int) Math.ceil(newVideoWidth);
        linearParams.height = (int) Math.ceil(newVideoHeight);
        videoCover.setLayoutParams(linearParams);
    }

    /**
     * ViewPager 内部 图片动态
     */
    public static void setImageHeight(Context activity, ImageView showImg, float imageW, float imageH, float vpH) {
        float scale = Constants.FOLLOW_PICTURE_SCALE;//默认显示宽高比
        float imageRatio = imageW / imageH;//图片宽高比
        float mScreenWidth = getScreenWidth(activity) - dip2px(activity, 28);//屏幕宽 - 两边的marge
        float newImageW = 0;
        float newImageH = 0;
        if (imageRatio >= scale) {
            newImageW = mScreenWidth;
            newImageH = newImageW / imageRatio;
        } else {
            newImageH = vpH;
            newImageW = newImageH * imageRatio;
        }
        setLayoutParams(showImg, (int) Math.ceil(newImageW), (int) Math.ceil(newImageH));
    }

    public static void setVideoHeight(Context context, String videoCover, ImageView coverView, VideoView videoView) {
        float videoHeight = Float.parseFloat(ScreenUtils.getVideoHight(videoCover));
        float videoWidth = Float.parseFloat(ScreenUtils.getVideoWidth(videoCover));
        float scale = Constants.FOLLOW_PICTURE_SCALE;//默认显示宽高比
        float videoRatio = videoWidth / videoHeight;//视频宽高比
        float mScreenWidth = getScreenWidth(context) - dip2px(context, 28);//屏幕宽 - 两边的marge
        float newVideoW = 0;
        float newVideoH = 0;
        if (videoRatio >= scale) {
            newVideoW = mScreenWidth;
            newVideoH = newVideoW / videoRatio;
        } else {
            newVideoH = (float) (mScreenWidth * 0.75);
            newVideoW = newVideoH * videoRatio;
        }
        setLayoutParams(coverView, (int) Math.ceil(newVideoW), (int) Math.ceil(newVideoH));
        setLayoutParams(videoView, (int) Math.ceil(newVideoW), (int) Math.ceil(newVideoH));
    }

    public static void setLayoutParams(View view, int width, int height) {
        ViewGroup.LayoutParams linearParams = view.getLayoutParams();
        linearParams.width = width;
        linearParams.height = height;
        view.setLayoutParams(linearParams);
    }

    /**
     * 根据指定比例，测量view高和宽
     *
     * @param viewHeight   图片的高
     * @param viewWidth    图片的宽
     * @param measureScale 比例相除得值
     * @return int[2]  0 = 宽  1 = 高
     */
    public static int[] measureViewHW(Context context, float viewHeight, float viewWidth, float measureScale, int margeSum) {
        int[] ints = new int[2];
        float mCutRatio = 0;//宽高缩放比
        float mScreenWidth = getScreenWidth(context) - margeSum;//屏幕宽 - 两边的marge
        float newVideoWidth = 0;
        float newVideoHeight = 0;
        if (viewHeight == 0) {
            viewHeight = getScreenWidth();
        }
        if (viewWidth / viewHeight < measureScale) {
            if (viewWidth > mScreenWidth) {
                mCutRatio = viewWidth / mScreenWidth;//计算出裁剪比例
                newVideoHeight = viewHeight / mCutRatio;
                newVideoWidth = mScreenWidth;
                //例如measureScale = 3:4->如果宽/高不是3:4比例，说明不是图片比例不是3:4。因为宽固定了，所以只能改变高，达到measureScale
                if (newVideoWidth / newVideoHeight != measureScale) {
                    newVideoHeight = newVideoWidth / measureScale;
                }

            } else if (viewWidth <= mScreenWidth) {
                newVideoWidth = mScreenWidth;//这里的赋值其实没有意义，只为最后统一使用  newVideoHeight、newVideoWidth
                mCutRatio = mScreenWidth / viewWidth;
                newVideoHeight = viewHeight * mCutRatio;
                if (newVideoHeight > mScreenWidth / measureScale) {
                    //如果View高，大于屏幕宽/measureScale
                    newVideoHeight = mScreenWidth / measureScale;//
                } /*else {
                    newVideoHeight = viewHeight;
                }*/
            }
        } else {
            newVideoHeight = viewHeight;
            newVideoWidth = viewWidth;
            //以下不足一屏扩充，大于一屏缩小
            if (newVideoWidth < mScreenWidth) {
                //扩充一屏，高按比例扩大
                mCutRatio = mScreenWidth / newVideoWidth;
                newVideoHeight = newVideoHeight * mCutRatio;
                newVideoWidth = mScreenWidth;
            } else if (newVideoWidth > mScreenWidth) {
                //缩放到一屏大小，高按比例缩小
                mCutRatio = newVideoWidth / mScreenWidth;
                newVideoHeight = newVideoHeight / mCutRatio;
                newVideoWidth = mScreenWidth;
            }
        }
        ints[0] = (int) Math.ceil(newVideoWidth);
        ints[1] = (int) Math.ceil(newVideoHeight);
        return ints;
    }



    /**
     * 关注列表 视频动态
     */
    public static void getItemHeight(RelativeLayout rlVideo, ImageView videoCover, VideoView videoView, View mantleView, float videoHeight, float videoWidth, float followHeight, Context activity) {
        float mCutRatio = 0;//宽高比例
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        int mBottomTabHeight = DensityUtil.dip2px(activity, 60) * 2;//这里的 *2  包含的底部的tab  和 顶部的状态栏
        float maxVideoHeight = (int) (mScreenHeight - mBottomTabHeight - followHeight - (mScreenHeight - mBottomTabHeight - followHeight) * 0.2);//视频展示的最大高度

        float newVideoWidth = 0;
        float newVideoHeight = 0;

        if (videoWidth >= mScreenWidth) {//如果视频宽度超过一屏幕
            mCutRatio = videoWidth / mScreenWidth;
            newVideoHeight = (videoHeight / mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {//如果计算完之后的视频的高大于最大高，换一种计算形式
                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = (videoWidth / mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }

            }
            setMantleView(rlVideo, videoCover, videoView, mantleView, mScreenWidth, newVideoWidth, newVideoHeight);
        } else if (videoWidth < mScreenWidth) {//如果视频宽度不超过一屏幕，铺满屏幕，等比例放大高，，如果高超过一屏幕，按照最大高设置;

            mCutRatio = mScreenWidth / videoWidth;
            newVideoHeight = videoHeight * mCutRatio;
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {

                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = videoWidth / mCutRatio;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
            }
        }
        setMantleView(rlVideo, videoCover, videoView, mantleView, mScreenWidth, newVideoWidth, newVideoHeight);
    }

    /**
     * 关注列表 视频动态
     */
    public static void getVideoHeight(Context activity, String url, RelativeLayout rlVideo, ImageView videoCover, ImageView mantleView, VideoView videoView) {
        float mCutRatio = 1.78f;//宽高比例
        float rlWidth = (ScreenUtils.getScreenWidth(activity) - ScreenUtils.dpToPx(28));
        float rlHeight = rlWidth / mCutRatio;
        float maxVideoHeight = rlHeight;

        ViewGroup.LayoutParams layoutParams = rlVideo.getLayoutParams();
        float width = rlWidth;
        layoutParams.height = (int) (width / mCutRatio);
        rlVideo.setLayoutParams(layoutParams);

        getImageHeight(activity, videoCover, mCutRatio);
        getImageHeight(activity, mantleView, mCutRatio);

        int videoHeight = Integer.parseInt(ScreenUtils.getVideoHight(url));
        int videoWidth = Integer.parseInt(ScreenUtils.getVideoWidth(url));
        float newVideoHeight = 0;
        float newVideoWidth = 0;
        if (videoWidth >= rlWidth) {//如果视频宽度超过一屏幕
            mCutRatio = videoWidth / rlWidth;
            newVideoHeight = (videoHeight / mCutRatio);
            newVideoWidth = rlWidth;
            if (newVideoHeight > maxVideoHeight) {//如果计算完之后的视频的高大于最大高，换一种计算形式
                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = (videoWidth / mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
            }
        } else if (videoWidth < rlWidth) {//如果视频宽度不超过一屏幕，铺满屏幕，等比例放大高，，如果高超过一屏幕，按照最大高设置;
            mCutRatio = rlWidth / videoWidth;
            newVideoHeight = videoHeight * mCutRatio;
            newVideoWidth = rlWidth;
            if (newVideoHeight > maxVideoHeight) {
                if (videoHeight > maxVideoHeight) {
                    mCutRatio = videoHeight / maxVideoHeight;
                    newVideoWidth = videoWidth / mCutRatio;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    mCutRatio = maxVideoHeight / videoHeight;
                    newVideoWidth = (videoWidth * mCutRatio);
                    newVideoHeight = maxVideoHeight;
                }
            }
        }
        RelativeLayout.LayoutParams mVideoViewLinearParams = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
        mVideoViewLinearParams.width = (int) Math.ceil(newVideoWidth);
        mVideoViewLinearParams.height = (int) Math.ceil(newVideoHeight);
        videoView.setLayoutParams(mVideoViewLinearParams);
    }

    private static void getImageHeight(Context context, ImageView videoCover, float mCutRatio) {
        ViewGroup.LayoutParams layoutParams = videoCover.getLayoutParams();
        int width = (ScreenUtils.getScreenWidth(context) - ScreenUtils.dpToPx(28));
        layoutParams.height = (int) (width / mCutRatio);
        videoCover.setLayoutParams(layoutParams);
    }



    /**
     * 约局顶部banner
     *
     * @return
     */
    public static int setBannerViewPagerHeight(
            View videoCover, float videoHeight, float videoWidth, float followListHeadHeight, Context activity) {
        float mCutRatio = 0;//宽高比例
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        int maxVideoHeight = (int) (mScreenHeight - followListHeadHeight - ((mScreenHeight - followListHeadHeight) * 0.2));//展示的最大高度

        float newVideoWidth = 0;
        float newVideoHeight = 0;
        //这里得宽是ViewPager得宽,,ViewPager永远充满一屏幕
        if (videoWidth >= mScreenWidth) {//如果视频宽度超过一屏幕
            mCutRatio = videoWidth / mScreenWidth;//计算缩放比例
            newVideoHeight = (videoHeight / mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {//匹配完宽读，，计算的高度超过最大高度。。则换计算方法，优先匹配高宽
                if (videoHeight >= maxVideoHeight) {//这里的宽不需要压缩
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = videoHeight;
                }

            }

        } else {//如果视频宽度不超过一屏幕，铺满屏幕，等比例放大高，，如果高超过一屏幕，按照最大高设置
            mCutRatio = mScreenWidth / videoWidth;
            newVideoHeight = (videoHeight * mCutRatio);
            newVideoWidth = mScreenWidth;
            if (newVideoHeight > maxVideoHeight) {//如果宽大于最大宽了
                if (videoHeight > maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = maxVideoHeight;
                }
                if (videoHeight < maxVideoHeight) {
                    newVideoWidth = mScreenWidth;
                    newVideoHeight = videoHeight;
                }
            }
        }
        ViewGroup.LayoutParams linearParams = videoCover.getLayoutParams();
        linearParams.width = (int) Math.ceil(newVideoWidth);
        linearParams.height = (int) Math.ceil(newVideoHeight);

        videoCover.setLayoutParams(linearParams); //使设置好的布局参数应用到控件


        return (int) Math.ceil(newVideoHeight);
    }


    public static void setMaterialPictureView(Activity activity, ImageView imageView, float originalHeight, float originalWidth) {
        int mScreenHeight = getScreenHeight(activity);//屏幕高
        int mScreenWidth = getScreenWidth(activity);//屏幕宽
        float mCutRatio = 0;//宽高比例
        float newVideoWidth = 0;
        float newVideoHeight = 0;
        if (originalWidth > mScreenWidth) {
            mCutRatio = originalWidth / mScreenWidth;
            newVideoHeight = originalHeight / mCutRatio;
            newVideoWidth = mScreenWidth;
        } else if (originalWidth < mScreenWidth) {
            mCutRatio = mScreenWidth / originalWidth;
            newVideoHeight = originalHeight * mCutRatio;
            newVideoWidth = mScreenWidth;
        } else {
            newVideoWidth = originalWidth;
            newVideoHeight = originalHeight;
        }
        ViewGroup.LayoutParams mParams = imageView.getLayoutParams();
        mParams.width = (int) Math.ceil(newVideoWidth);
        mParams.height = (int) Math.ceil(newVideoHeight);
        imageView.setLayoutParams(mParams);
    }

    private static void setMantleView(RelativeLayout rlVideo, ImageView videoCover, VideoView videoView, View mantleView, float mScreenWidth, float newVideoWidth, float newVideoHeight) {

        LinearLayout.LayoutParams mRlVideoLinearParams = (LinearLayout.LayoutParams) rlVideo.getLayoutParams();
        mRlVideoLinearParams.height = (int) Math.ceil(newVideoHeight);
        mRlVideoLinearParams.width = (int) Math.ceil(mScreenWidth);
        rlVideo.setLayoutParams(mRlVideoLinearParams);

        RelativeLayout.LayoutParams mVideoCoverLinearParams = (RelativeLayout.LayoutParams) videoCover.getLayoutParams();
        mVideoCoverLinearParams.width = (int) Math.ceil(newVideoWidth);
        mVideoCoverLinearParams.height = (int) Math.ceil(newVideoHeight);
        videoCover.setLayoutParams(mVideoCoverLinearParams);

        RelativeLayout.LayoutParams mVideoViewLinearParams = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
        mVideoViewLinearParams.width = (int) Math.ceil(newVideoWidth);
        mVideoViewLinearParams.height = (int) Math.ceil(newVideoHeight);

        videoView.setLayoutParams(mVideoViewLinearParams);
    }

    /**
     * 可见屏幕高度
     **/
    public static int getAppHeight(Activity paramActivity) {
        Rect localRect = new Rect();
        paramActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        return localRect.height();
    }

    /**
     * 屏幕分辨率高
     **/
    public static int getScreenHeight(Activity paramActivity) {
        Display display = paramActivity.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        return metrics.heightPixels;
    }

    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    public static int getScreenWidth(Context context) {
        if (context != null) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            return dm.widthPixels;
        } else {
            return 0;
        }
    }

    public static int getScreenX(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        return size.x;
    }

    public static int getScreenY(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        return size.y;
    }

    public static void setLayoutParams(View videoCover, View backView, int width, int height) {
        ViewGroup.LayoutParams linearParams = videoCover.getLayoutParams();

        linearParams.width = width;
        linearParams.height = height;

        videoCover.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        if (backView != null) {
            backView.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams linearParams2 = backView.getLayoutParams();
            linearParams2.width = ViewGroup.LayoutParams.MATCH_PARENT;
            linearParams2.height = height;
            backView.setLayoutParams(linearParams2); //使设置好的布局参数应用到控件
        }
    }


    public static int marginSum(Activity activity, int... margin) {
        int marginSumWidth = 0;
        for (int j : margin) {
            marginSumWidth = marginSumWidth + j;
        }
        return ScreenUtils.dip2px(activity, marginSumWidth);
    }


    public static int getUserNameMaxWidth(Activity activity, int marginSum, View... userHead) {
        int viewSumWidth = 0;
        int screenWidth = ScreenUtils.getScreenWidth(activity);
        for (View view : userHead) {
            //Log.d("======", "getUserNameMaxWidth: " + view.getMeasuredWidth());
            viewSumWidth += view.getMeasuredWidth();
        }
        return screenWidth - viewSumWidth - marginSum;
    }










    /**
     * statusBar高度
     **/
    public static int getStatusBarHeight(Activity paramActivity) {
        Rect localRect = new Rect();
        paramActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        return localRect.top;

    }

    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static void sendPhoneShock(Context context, long shockTime) {
        if (shockTime == 0) {
            shockTime = 500;
        }
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(shockTime);
    }

    public static String interceptStr(String des, int indexOf) {
        if (TextUtils.isEmpty(des)) {
            des = "";
        }
        if (des.length() == indexOf)
            return des;
        indexOf = indexOf - 1;
        if (des.length() > indexOf) {
            des = des.substring(0, indexOf);
            des = des + "...";
        }
        return des;
    }

    /**
     * @param activity
     * @param slidingHeight
     * @param scrollY
     * @param titleFather
     * @param titleView     标题栏中存在的Img 集合
     *                      Img在xml中 必须时src 滑动时才会发生颜色变化
     */
    public static void setTitleGradients(Activity activity, int slidingHeight, int scrollY, View titleFather, ImageView... titleView) {
        //这个就是关键方法了，大家可根据实际需要进行编写
        if (scrollY <= 0) {
            //滑动到最顶部
            if (!StatusBarUtil.setStatusBarDarkTheme(activity, false)) {
                StatusBarUtil.setStatusBarColor(activity, R.color.white);
            }

            titleFather.setBackgroundColor(Color.argb(0, 255, 255, 255));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                for (ImageView view : titleView) {
                    view.setImageTintList(ColorStateList.valueOf(Color.argb(255, 255, 255, 255)));
                }
            }
        } else if (scrollY > 0 && scrollY <= slidingHeight) {
            float scale = (float) scrollY / slidingHeight;
            float alpha = (255 * scale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                for (ImageView imageView : titleView) {
                    imageView.setImageTintList(ColorStateList.valueOf(Color.argb((int) alpha, 0, 0, 0)));
                }
            }
            titleFather.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
        } else {
            //当滑动距离 到达 slidingHeight 峰值
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                for (ImageView imageView : titleView) {
                    imageView.setImageTintList(ColorStateList.valueOf(Color.argb(255, 0, 0, 0)));
                }
            }
            if (!StatusBarUtil.setStatusBarDarkTheme(activity, true)) {
                StatusBarUtil.setStatusBarColor(activity, R.color.black);
            }
            titleFather.setBackgroundColor(Color.argb(255, 255, 255, 255));
        }
    }

    public static int dpToPx(int dp) {
        return dpToPx((float) dp);
    }

    public static int dpToPx(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, sDM);
    }

    public static int spToPx(float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, sDM);
    }

    public static int pxToDp(int px) {
        return Math.round(px / getDensity());
    }


}
